home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-05-01 | 17.2 KB | 637 lines | [TEXT/MPS ] |
- // UAppleEvents.cp
- // Copyright © 1988-1991 by Apple Computer Inc. All rights reserved.
-
-
- #ifndef __UAPPLEEVENTS__
- #include <UAppleEvents.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __UFAILURE__
- #include <UFailure.h>
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include <UMacAppUtilities.h>
- #endif
-
- #ifndef __UAPPLICATION__
- #include <UApplication.h>
- #endif
-
- #ifndef __UIterator__
- #include <UIterator.h>
- #endif
-
- struct MAEventTableRec
- {
- OSType theClass;
- OSType theID;
- long theValue;
- };
-
-
- typedef MAEventTableRec* MAEventTablePointer;
-
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal OSErr AppleEventsDispatch(const AppleEvent& message,
- const AppleEvent& reply,
- long info)
- {
- // Dispatch through a method so that the behavior can be changed
- if (gApplication != NULL)
- return gApplication->DispatchAppleEvent(message, reply, info);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- pascal void InitUAppleEvents(void)
- {
- if (qNeedsAppleEventMgr || gConfiguration.hasAppleEventMgr)
- {
- FailInfo fi;
- short numberOfTables;
- Size tableSize;
- short tableElements;
- Handle tableHandle = NULL;
- MAEventTablePointer tablePtr;
- short eventIndex;
- SignedByte savedState;
-
- VOLATILE(tableHandle);
-
- // Look for all AppleEvent dispatch tables…
- numberOfTables = Count1Resources('aedt');// Count the number of 'aedt' resources
- FailResError();
-
- if (fi.Try())
- {
- for (short tableIndex = 1; tableIndex <= numberOfTables; ++tableIndex)
- {
- tableHandle = Get1IndResource('aedt', tableIndex);
- FailResError();
-
- savedState = LockHandleHigh(tableHandle);
-
- tableSize = GetHandleSize(tableHandle);
- FailMemError();
-
- tableElements = (short)(tableSize / sizeof(MAEventTableRec));
-
- tablePtr = (MAEventTablePointer) * tableHandle;
-
- for (eventIndex = 1; eventIndex <= tableElements; ++eventIndex)
- {
- // Install a single event handler for all events
- FailOSErr(AEInstallEventHandler(tablePtr->theClass, tablePtr->theID, (EventHandlerProcPtr) & AppleEventsDispatch, tablePtr->theValue, FALSE));
- ++tablePtr;
- }
-
- HSetState(tableHandle, savedState);
- ReleaseResource(tableHandle);
- }
- fi.Success();
- }
- else // Recover
- {
- if (tableHandle)
- ReleaseResource(tableHandle);
- fi.ReSignal();
- }
- }
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEInit
-
- pascal void TAppleEvent::IAppleEvent(const AEEventClass itsEventClass,
- const AEEventID itsEventID,
- const AEAddressDesc& itsAddress,
- long itsSendingMode)
- {
- FailInfo fi;
- AppleEvent theMessage;
-
- fMessage.dataHandle = NULL;
-
- this->IObject();
-
- if (fi.Try())
- {
- FailOSErr(AECreateAppleEvent(itsEventClass, itsEventID, itsAddress, kAutoGenerateReturnID, kAnyTransactionID, theMessage));
- fi.Success();
- }
- else // Recover
- {
- this->Free();
- fi.ReSignal();
- }
-
- fMessage = theMessage;
- fSendingMode = itsSendingMode;
- fPriority = kAENormalPriority;
- fTimeoutVal = kAEDefaultTimeout;
- fFreeMessage = TRUE; // We created it, we need to free it
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AEInit
-
- pascal void TAppleEvent::InitFromMessage(const AppleEvent& theMessage)
- {
- this->IObject();
-
- fMessage = theMessage;
- fSendingMode = kAENoReply;
- fPriority = kAENormalPriority;
- fTimeoutVal = kAEDefaultTimeout;
- fFreeMessage = FALSE; // We don't really own the message so let the
- // AppleEvent manager be responsible
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::Free(void) // override
- {
- if (fFreeMessage)
- AEDisposeDesc(fMessage);
-
- inherited::Free();
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::Send(void)
- {
- AppleEvent theMessage = fMessage;
- AppleEvent theReply;
-
- FailOSErr(AESend(theMessage, theReply, fSendingMode, fPriority, fTimeoutVal, NULL, NULL));
- // !!! We could use a good default cursor spinner here.
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::GetAddress(AEAddressDesc& theAddress)
-
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AEGetAttributeDesc(theMessage, keyAddressAttr, typeTargetID, theAddress));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal short TAppleEvent::GetPriority(void)
- {
- return fPriority;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal short TAppleEvent::GetReturnID(void)
- {
- AppleEvent theMessage = fMessage;
- DescType actualType;
- short returnID;
- long actualSize;
-
- FailOSErr(AEGetAttributePtr(theMessage, keyReturnIDAttr, typeShortInteger, actualType, (Ptr) & returnID, sizeof(short), actualSize));
- return returnID;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal long TAppleEvent::GetTimeoutVal(void)
- {
- return fTimeoutVal;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal long TAppleEvent::GetTransactionID(void)
- {
- AppleEvent theMessage = fMessage;
- long transactionID;
- DescType actualType;
- long actualSize;
-
- FailOSErr(AEGetAttributePtr(theMessage, keyTransactionIDAttr, typeLongInteger, actualType, (Ptr) & transactionID, sizeof(long), actualSize));
- return transactionID;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::SetAddress(const AEAddressDesc& theAddress)
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AEPutAttributeDesc(theMessage, keyAddressAttr, theAddress));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::SetPriority(short thePriority)
- {
- fPriority = thePriority;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::SetReturnID(short returnID)
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AEPutAttributePtr(theMessage, keyReturnIDAttr, typeShortInteger, (Ptr) & returnID, sizeof(short)));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::SetTimeoutVal(long theTimeoutVal)
- {
- fTimeoutVal = theTimeoutVal;
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::SetTransactionID(long transactionID)
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AEPutAttributePtr(theMessage, keyTransactionIDAttr, typeLongInteger, (Ptr) & transactionID, sizeof(long)));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::ReadInteger(const AEKeyword theKey,
- short& theData)
- {
- AppleEvent theMessage = fMessage;
- DescType actualType;
- long actualSize;
- OSErr theErr;
-
-
- theErr = AEGetParamPtr(theMessage, theKey, typeShortInteger, actualType, (Ptr) & theData, sizeof(short), actualSize);
-
- if ((theErr != noErr) && (theErr != errAEDescNotFound))
- FailOSErr(theErr);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::ReadLong(const AEKeyword theKey,
- long& theData)
- {
- AppleEvent theMessage = fMessage;
- DescType actualType;
- long actualSize;
- OSErr theErr;
-
-
- theErr = AEGetParamPtr(theMessage, theKey, typeLongInteger, actualType, (Ptr) & theData, sizeof(long), actualSize);
-
- if ((theErr != noErr) && (theErr != errAEDescNotFound))
- FailOSErr(theErr);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::ReadString(const AEKeyword theKey,
- Str255& theData)
- {
- AppleEvent theMessage = fMessage;
- DescType actualType;
- long actualSize;
- OSErr theErr;
-
- theErr = AEGetParamPtr(theMessage, theKey, typeChar, actualType, (Ptr)&theData, sizeof(theData), actualSize);
-
- if ((theErr != noErr) && (theErr != errAEDescNotFound))
- FailOSErr(theErr);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::ReadSectionHandle(const AEKeyword theKey,
- SectionHandle& theData)
- {
- AppleEvent theMessage = fMessage;
- DescType actualType;
- long actualSize;
- OSErr theErr;
-
-
- theErr = AEGetParamPtr(theMessage, theKey, typeSectionH, actualType, (Ptr) & theData, sizeof(SectionHandle), actualSize);
-
- if ((theErr != noErr) && (theErr != errAEDescNotFound))
- FailOSErr(theErr);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::ReadPtrList(const AEKeyword theKey,
- const DescType theType,
- TDynamicArray* theData)
- {
- FailInfo fi;
- Ptr thePtr = NULL;
-
- VOLATILE(thePtr);
-
- if (fi.Try())
- {
- AppleEvent theMessage = fMessage;
- AEDescList theDescList;
- OSErr theErr;
-
- theErr = AEGetParamDesc(theMessage, theKey, typeAEList, theDescList);
- if (theErr == noErr)
- {
- long items;
- AEKeyword theActualKey;
- DescType theActualType;
- long theActualSize;
-
- FailOSErr(AECountItems(theDescList, items));// Get the number of elements in the list
- thePtr = NewPtr(theData->fElementSize);
- FailMemError();
-
- for (short index = 1; index <= items; ++index)
- {
- FailOSErr(AEGetNthPtr(theDescList, index, theType, theActualKey, theActualType, thePtr, theData->fElementSize, theActualSize));
-
- // !!! We should probably check that theActualSize matches theMaxSize. However it would
- // really be an error only if the data type is not a string. Currently this is an
- // unresolved issue with the AppleEvent manager. It may in the future return an error
-
- theData->InsertElementsBefore(theData->GetSize() + 1, thePtr, 1);
- }
- }
- else if (theErr != errAEDescNotFound)
- FailOSErr(theErr);
- fi.Success();
- }
- else // Recover
- {
- thePtr = DisposeIfPtr(thePtr);
- fi.ReSignal();
- }
- thePtr = DisposeIfPtr(thePtr);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::ReadHandleList(const AEKeyword theKey,
- const DescType theType,
- TDynamicArray* theData)
- {
- AppleEvent theMessage = fMessage;
- AEDescList theDescList;
- OSErr theErr;
-
- theErr = AEGetParamDesc(theMessage, theKey, typeAEList, theDescList);
- if (theErr == noErr)
- {
- AEDesc theHandle;
- AEKeyword theActualKey;
- long items;
-
- theHandle.dataHandle = NULL;
- FailOSErr(AECountItems(theDescList, items));// Get the number of elements in the list
- for (short index = 1; index <= items; ++index)
- {
- FailOSErr(AEGetNthDesc(theDescList, index, theType, theActualKey, theHandle));
- theData->InsertElementsBefore(theData->GetSize() + 1, (Ptr) & theHandle.dataHandle, 1);
- }
- }
- else if (theErr != errAEDescNotFound)
- FailOSErr(theErr);
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::ReadParameter(const AEKeyword theKey,
- const DescType desiredType,
- AEDesc& theData)
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AEGetParamDesc(theMessage, theKey, desiredType, theData));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::ReadParameterPtr(const AEKeyword theKey,
- const DescType desiredType,
- DescType actualType,
- Ptr theData,
- long maximumSize,
- long& actualSize)
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AEGetParamPtr(theMessage, theKey, desiredType, actualType, theData, maximumSize, actualSize));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::WriteInteger(const AEKeyword theKey,
- short theData)
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AEPutParamPtr(theMessage, theKey, typeShortInteger, (Ptr) & theData, sizeof(short)));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::WriteLong(const AEKeyword theKey,
- long theData)
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AEPutParamPtr(theMessage, theKey, typeLongInteger, (Ptr) & theData, sizeof(long)));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::WriteString(const AEKeyword theKey,
- const Str255& theData)
- {
- AppleEvent theMessage = fMessage;
- Str255 localString = theData;
-
- FailOSErr(AEPutParamPtr(theMessage, theKey, typeChar, (Ptr) &localString, sizeof(localString)));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::WriteSectionHandle(const AEKeyword theKey,
- SectionHandle theData)
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AEPutParamPtr(theMessage, theKey, typeSectionH, (Ptr) & theData, sizeof(SectionHandle)));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::WritePtrList(const AEKeyword theKey,
- const DescType theType,
- TDynamicArray* theData)
- {
- FailInfo fi;
- AEDescList theDescList;
-
- theDescList.dataHandle = NULL;
- if (fi.Try())
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AECreateList(NULL, 0, FALSE, theDescList));
-
- {
- // Create a block for the iterator to disambiguate it from the failure handler it's nested in
- CArrayIterator iter(theData);
-
- for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
- FailOSErr(AEPutPtr(theDescList, (long)i, theType, theData->ComputeAddress(i), theData->fElementSize));
- }
- FailOSErr(AEPutParamDesc(theMessage, theKey, theDescList));
- fi.Success();
- }
- else // Recover
- {
- FailOSErr(AEDisposeDesc(theDescList));
- fi.ReSignal();
- }
-
- FailOSErr(AEDisposeDesc(theDescList));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::WriteHandleList(const AEKeyword theKey,
- const DescType theType,
- TDynamicArray* theData)
- {
- FailInfo fi;
- AEDescList theDescList;
-
- theDescList.dataHandle = NULL;
- if (fi.Try())
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AECreateList(NULL, 0, FALSE, theDescList));
-
- {
- // Create a block for the iterator to disambiguate it from the failure handler it's nested in
- CArrayIterator iter(theData);
-
- for (ArrayIndex i = iter.FirstIndex(); iter.More(); i = iter.NextIndex())
- {
- AEDesc theElement;
-
- theElement.dataHandle = NULL;
- theElement.descriptorType = theType;
- theElement.dataHandle = *((Handle *)(theData->ComputeAddress(i)));
- FailOSErr(AEPutDesc(theDescList, (long)i, theElement));
- }
- }
-
- FailOSErr(AEPutParamDesc(theMessage, theKey, theDescList));
- fi.Success();
- }
- else // Recover
- {
- FailOSErr(AEDisposeDesc(theDescList));
- fi.ReSignal();
- }
-
- FailOSErr(AEDisposeDesc(theDescList));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::WriteParameter(const AEKeyword theKey,
- const AEDesc& theData)
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AEPutParamDesc(theMessage, theKey, theData));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::WriteParameterPtr(const AEKeyword theKey,
- const DescType typeCode,
- Ptr theData,
- long dataSize)
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AEPutParamPtr(theMessage, theKey, typeCode, theData, dataSize));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment AERes
-
- pascal void TAppleEvent::DeleteParameter(const AEKeyword theKey)
- {
- AppleEvent theMessage = fMessage;
-
- FailOSErr(AEDeleteKeyDesc(theMessage, theKey));
- }
-
- //--------------------------------------------------------------------------------------------------
- #pragma segment MAFields
-
- pascal void TAppleEvent::Fields(TObject* obj) // override
- {
- obj->DoToField("TAppleEvent", (Ptr)NULL, bClass);
- obj->DoToField("fMessage", (Ptr) & fMessage, bHandle);
- obj->DoToField("fSendingMode", (Ptr) & fSendingMode, bLongInt);
- obj->DoToField("fPriority", (Ptr) & fPriority, bInteger);
- obj->DoToField("fTimeoutVal", (Ptr) & fTimeoutVal, bLongInt);
- inherited::Fields(obj);
- }
-
-
-